package znbkTopicType.resContent;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.view.widget.VoicePlayer;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.List;

import znbkTopicType.WrapBaseAdapter;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperInfor.ZnbkPaperInfor;
import znbkTestPaperRdtz.AudioTextJson;

import static znbkTestPaperBj.activity.NotePaperActivity.mEntityBj;
import static znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity.mEntityRdtz;
import static znbkTestPaperSc.activity.CollectionPaperActivity.mEntitySc;

/**
 * Created by Administrator on 2018\3\15 0015.
 */

public class ZnbkContentResYplAdapter extends WrapBaseAdapter {

    private LayoutInflater mInflater;
    private Context mContext;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试卷的ID
    private int PaperID;
    private int ModelFrom;//试卷来源


    private ViewHolder holder = null;
    private class ViewHolder{
        WebView WvTheme;//大题文本题干区
        ImageButton BtnPlayPause;
        TextView TvAnswerTime;
        SeekBar SbMusic;

        private ViewHolder(View convertView) {
            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);
            BtnPlayPause = (ImageButton) convertView.findViewById(R.id.btn_start);
            TvAnswerTime = (TextView) convertView.findViewById(R.id.tv_answerTime);
            SbMusic = (SeekBar) convertView.findViewById(R.id.music_progress);
        }
    }

    /*
   * 获取小题选项信息
   * */
    public ZnbkContentResYplAdapter(Context context, int sumItem, int paperID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.ModelFrom = modelFrom;
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_content_ypl_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /*
        * 获取作文题用户相关信息
        * */
        getQuesInfor();

        /*
        * 显示题干信息
        * */
        showQuesInfor();


        return convertView;
    }


    /*
    * 获取作文题用户相关信息
    * */
    private String ResName = "";
    private String AudioPath = "";
    private String TextPath = "";
    private String htmlStr = "";
    private void  getQuesInfor(){

        if(ModelFrom== ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION) {

                /*
                * 音频路径
                * */
                    AudioPath = mEntityRdtz.getResFromTK().getAudioList().get(0).getAudioPath();

                /*
                * 音频对应的文本路径
                * */
                    TextPath = mEntityRdtz.getResFromTK().getAudioList().get(0).getTextPath();

        }else if(ModelFrom==ZnbkPaperDef.FROM_COLLECTION){


                /*
                * 音频路径
                * */
                    AudioPath = mEntitySc.getResDetail().getAudioList().get(0).getAudioPath();

                /*
                * 音频对应的文本路径
                * */
                    TextPath = mEntitySc.getResDetail().getAudioList().get(0).getTextPath();

        }else if(ModelFrom==ZnbkPaperDef.FROM_NOTE){

                /*
                * 音频路径
                * */
            AudioPath = mEntityBj.getModelTKQue().getAudioList().get(0).getAudioPath();

                /*
                * 音频对应的文本路径
                * */
            TextPath = mEntityBj.getModelTKQue().getAudioList().get(0).getTextPath();

        }

    }

    /*
    * 显示界面信息
    * */
    private void showQuesInfor(){

        /*
        * 初始化音频播放器
        * */
        mVoicePlayer = new VoicePlayer(holder.SbMusic,AudioPath,holder.TvAnswerTime,holder.BtnPlayPause,true);

         /*
        * 显示音频原文
        * */
        showYplText(TextPath);
    }


    private void showYplText(String Path){

        String url = ZnbkPaperInfor.BaseHttpAdress + "api/ZNBK/W_ZNBK_GetTextInfo?" + "path=" + Path + "&IsAbsoluteUrl=true";

        /**
         * OkHttpClient 的Get请求
         */

        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
            }

            @Override
            public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                htmlStr = response.body().string();
                Log.e("==wgl==", "" + htmlStr);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mHandler.sendEmptyMessage(100);
                    }
                }).start();
            }

        });
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100) {
                Gson gson = new Gson();
                List<AudioTextJson> listAudioTextJson = gson.fromJson(htmlStr, new TypeToken<List<AudioTextJson>>(){}.getType());
                String audioTextAll = "";
                for(int i=0;i<listAudioTextJson.size();i++){

                    audioTextAll = audioTextAll + listAudioTextJson.get(i).getContent()+"</P>";

                }
                holder.WvTheme.loadData(audioTextAll,"text/html","UTF-8");
            }
        }
    };

}